﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using SharedClasses;
using System.ComponentModel.Design.Serialization;
using UIDesigner.SurfaceProperties.Operations;
using UIDesigner.SurfaceProperties.Information;
using AFSharedLibrary.SharedControls.Framework;

namespace UIDesigner.SurfaceProperties.Templates
{
    [Serializable()]
    public partial class CreateOptionsTemplate : UserControl, ITemplate, ITemplateOptionsPanel
    {
        private CreateProperties m_objCreateProperties;
        private AFDesignerHost m_objHost;
        private bool m_bReset;
        private bool m_bHasInstantiated;
        private string m_strFirstTabControlName;

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string FirstTabControlName
        {
            get
            {
                return this.FirstTabControl.Name;
            }

            set
            {
                m_strFirstTabControlName = value;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Control FirstTabControl
        {
            get
            {
                return null;
            }

            set
            {

            }
        }

        public CreateProperties CreateProperties
        {
            get { return m_objCreateProperties; }
            set { m_objCreateProperties = value; }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public AFDesignerHost Host
        {
            get { return m_objHost; }
            set { m_objHost = value; }
        }

        public CreateOptionsTemplate()
        {

        }

        public CreateOptionsTemplate(AFDesignerHost objHost, CreateProperties objCreateProperties)
        {
            m_objCreateProperties = objCreateProperties;
            m_objHost = objHost;

            InitializeComponent();
        }

        private INameCreationService NameCreationService
        {
            get
            {
                return (INameCreationService)((IServiceProvider)m_objHost).GetService(typeof(INameCreationService)); 
            }
        }

        public string TemplateName
        {
            get
            {
                return "CreateOptionsTemplate";
            }
        }

        public string HeaderText
        {
            get
            {
                return m_objCreateProperties.ObjectName + " \"Create New\" Options";
            }
        }

        public void RemoveControls()
        {
            RemoveControls(this);
        }

        public void RemoveControls(Control ctrlParent)
        {
            IContainer objContainer = (IContainer)m_objHost;

            foreach (Control ctrlControl in ctrlParent.Controls)
            {
                RemoveControls(ctrlControl);

                ((IContainer)m_objHost).Remove(ctrlControl);

                ctrlControl.Dispose();
            }

            objContainer.Remove(ctrlParent);
            ctrlParent.Dispose();
        }

        public void Reset()
        {
            this.GroupBox = (GroupBox) m_objHost.FindControlByName(this, "GroupBox");
            this.OptionsPanel = (TableLayoutPanel)m_objHost.FindControlByName(this, "OptionsPanel");

            m_bReset = true;
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool HasInstantiated 
        {
            get
            {
                return m_bHasInstantiated;
            }
        }

        public void InstantiateIn(Control container, Point location)
        {
            int nMaxWidth = 0;
            int nTotalHeight = 0;
            WindowsAPI.SIZEAPI sz = new WindowsAPI.SIZEAPI();
            int hwnd = GroupBox.Handle.ToInt32();
            int hdc = WindowsAPI.GetDC(hwnd);
            int nWidth;
            int nHeight;
            int hOldFont;
            int hFont = GroupBox.Font.ToHfont().ToInt32();
            string strName;

            this.Location = location;
            container.Controls.Add(this);

            if (m_bReset)
            {
                return;
            }

            strName = this.HeaderText;

            m_bHasInstantiated = true;
            this.GroupBox.Text = strName;

            OptionsPanel.RowStyles.Clear();
            OptionsPanel.ColumnStyles.Clear();
            OptionsPanel.RowCount = 0;

            hOldFont = WindowsAPI.SelectObject(hdc, hFont);

            WindowsAPI.GetTextExtentPoint32(hdc, ref strName, strName.Length, ref sz);
            WindowsAPI.ReleaseDC(hwnd, hdc);

            hOldFont = WindowsAPI.SelectObject(hdc, hOldFont);

            nMaxWidth = sz.cx;

            foreach (SurfaceConstructorInfo objConstructor in m_objCreateProperties.ConstructorInfos)
            {
                RadioButton ctrlRadioButton = new RadioButton();
                
                strName = objConstructor.ConstructorItemName;
                sz = new WindowsAPI.SIZEAPI();
                hwnd = ctrlRadioButton.Handle.ToInt32();
                hdc = WindowsAPI.GetDC(hwnd);
                hFont = ctrlRadioButton.Font.ToHfont().ToInt32();

                objConstructor.RadioButton = ctrlRadioButton;

                hOldFont = WindowsAPI.SelectObject(hdc, hFont); 

                WindowsAPI.GetTextExtentPoint32(hdc, ref strName, strName.Length, ref sz);
                WindowsAPI.ReleaseDC(hwnd, hdc);

                hOldFont = WindowsAPI.SelectObject(hdc, hOldFont); 

                ctrlRadioButton.Margin = new Padding(3);

                nWidth = sz.cx + ctrlRadioButton.Margin.Left + ctrlRadioButton.Margin.Right + 30;
                nHeight = sz.cy + ctrlRadioButton.Margin.Top + ctrlRadioButton.Margin.Bottom + 8;

                nMaxWidth = Math.Max(nMaxWidth, nWidth);
                nTotalHeight += nHeight;

                ctrlRadioButton.Name = "opt" + strName.Replace(", ", "");
                ctrlRadioButton.Text = strName;
                OptionsPanel.RowCount++;

                OptionsPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, nHeight));
                OptionsPanel.Controls.Add(ctrlRadioButton, 0, OptionsPanel.RowCount - 1);

                ctrlRadioButton.Tag = "0," + (OptionsPanel.RowCount - 1).ToString();
            }

            OptionsPanel.Width = nMaxWidth + 6;
            OptionsPanel.Height = nTotalHeight + 6;

            OptionsPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, nMaxWidth));

            foreach (RadioButton ctrlButton in OptionsPanel.Controls)
            {
                ctrlButton.Width = nMaxWidth;
            }

            GroupBox.Width = nMaxWidth + OptionsPanel.Margin.Left + OptionsPanel.Margin.Right + 10;
            GroupBox.Height = nTotalHeight + OptionsPanel.Margin.Top + OptionsPanel.Margin.Bottom + 22;

            this.Width = GroupBox.Width + GroupBox.Margin.Left + GroupBox.Margin.Right + 5;
            this.Height = GroupBox.Height + GroupBox.Margin.Top + GroupBox.Margin.Bottom + 5;

            GroupBox.Dock = DockStyle.Fill;
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<OptionToIndex> OptionToIndexMappings
        {
            get
            {
                List<OptionToIndex> rgMappings = new List<OptionToIndex>();

                foreach (SurfaceConstructorInfo objConstructor in m_objCreateProperties.ConstructorInfos)
                {
                    OptionToIndex objOptionToIndex = new OptionToIndex(objConstructor.RadioButton, objConstructor.PanelIndex);

                    rgMappings.Add(objOptionToIndex);
                }

                return rgMappings;
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ITemplatePanel SelectedPanel
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string LabelText
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public TemplatePanelRole Role
        {
            get
            {
                return TemplatePanelRole.Options;
            }
        }

        #region ITemplatePanel Members


        public string DebugOutput
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}
